$(function () {



  Formio.icons = 'fontawesome';
  Formio.createForm(document.getElementById('formio'),
    // 'http://192.168.1.138:8000/static/eform/formSchema.json'
    // JSON.parse(dataJson)
    dataJson,
    {
      readOnly: true,
      // renderMode: 'html'
    }
  ).then(function (form) {

    $('#check_as_other').on('click', function () {
      $('#choose-check-user-modal').modal('hide')
      $('#scan_modal').modal('show')
      instascan()
    })

    $('#final_check_as_other').on('click', function () {
      $("#eform").trigger('submit')
    })

    var deleteConfirm = new Confirm({
      body: "Type <strong>\"DELETE\"</strong> to delete the form.",
      type: 'prompt'
    })

    var resetConfirm = new Confirm({
      body: "Type <strong>\"RESET\"</strong> to reset the form state.",
      type: 'prompt'
    })

    console.log(data)
    form.submission = {
      data: data
    }
    // console.log(form._data)
    $('input[name="overall_test_result_radio"]').on('change', function () {
      $('#form-otr').val($(this).val())
    })

    $('#btn-reset').on('click', function (e) {
      resetConfirm.show((yes, p_value) => {
        if (yes && p_value == 'RESET') {
          setForm('reset')
          $('#eform').submit()
          console.log('yes')
        }
      })
    })

    $('#btn-delete').on('click', function (e) {
      console.log('delete form')
      deleteConfirm.show((yes, p) => {
        if (yes && p == "DELETE") {
          setForm('delete')
          $('#eform').trigger('submit')
        }
      })
    })

    $('.check-btn').on('click', function (e) {
      var action = $(this).val()
      setForm(action)
      $('#choose-check-user-modal').modal('show')
      // $('#eform').trigger('submit')
    })

    $('.save-btn').on('click', function (e) {
      console.log('save')
      var action = $(this).val()
      setForm(action)
      let fm = document.getElementById('eform')
      if (!fm.checkValidity()){
        setTimeout(_=>fm.reportValidity(),100)
        return
      }
      $('#eform').trigger('submit')
    })

    $('#check-current-user').on('click', function () {
      setForm($(this).val())
      $("#eform").trigger('submit')
    })



    function setForm(action) {
      $('#eform-action').val(action)
    }

  });

})